package com.ztsoft.user.service.project.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztsoft.user.mapper.project.PurchaserMapper;
import com.ztsoft.user.pojo.entity.project.Agency;
import com.ztsoft.user.pojo.entity.project.Purchaser;
import com.ztsoft.user.pojo.vo.project.ProjectMeta;
import com.ztsoft.user.service.project.PurchaserService;
import com.ztsoft.user.utils.Result;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PurchaserServiceImpl extends ServiceImpl<PurchaserMapper, Purchaser> implements PurchaserService {
    @Override
    public Result<Object> queryAllPurchaser(Purchaser purchaser, Integer currentPage, Integer pageSize) {
        Map<String, Object> resultMap = new HashMap<>();

        QueryWrapper<Purchaser> queryWrapper = new QueryWrapper<>(purchaser);

        // 分页查询
        IPage<Purchaser> page = this.page(new Page<>(currentPage, pageSize), queryWrapper);

        resultMap.put("count", page.getTotal());
        resultMap.put("data", page.getRecords());
        return Result.success(resultMap);
    }

    @Override
    public Result<Object> updatePurchaser(Purchaser purchaser) {
        boolean isSuccess = this.updateById(purchaser);
        if (isSuccess) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> addPurchaser(Purchaser purchaser) {
        boolean isSuccess = this.save(purchaser);
        if (isSuccess) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> deletePurchaser(String id) {
        boolean isSuccess = this.removeById(id);
        if (isSuccess) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> getPurchaserMeta() {
        List<Purchaser> purchaserList = list();
        ProjectMeta[] projectMetas = new ProjectMeta[purchaserList.size()];

        List<ProjectMeta> projectMetaArrayList =new ArrayList<>();

        for (Purchaser purchaser : purchaserList) {
            ProjectMeta projectMeta = new ProjectMeta();
            projectMeta.setValue(purchaser.getId());
            projectMeta.setLabel(purchaser.getPurchaserName());
            projectMetaArrayList.add(projectMeta);
        }

        return Result.success(projectMetaArrayList.toArray(projectMetas));
    }
}
